/**
 *
 * Company: Easy with
 * Official website: http://www.bankbigdata.cn/
 * project: sec-bigdata
 * Author:  Yang Huijing  2021-08-19
 * Email: 1073602@qq.com
 */
package com.poly.common.core.utils;


import com.poly.common.core.exception.base.BaseException;
import com.poly.common.core.tools.utils.Func;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 断言工具
 */
public class AssertUtil {

    public AssertUtil() {
    }
    public static <T extends BaseException,E extends Enum> void throwException(Class<T> clazz, E e){
        try {
            Class<?>[] classTypes= new Class[]{e.getClass()};
            Constructor<T> con=clazz.getConstructor(classTypes);
            throw con.newInstance(e);
        }catch (IllegalAccessException | InstantiationException | NoSuchMethodException |InvocationTargetException exc){
            exc.printStackTrace();
        }
    }

    public static <T extends BaseException,E extends Enum> void isTrue(boolean expression, Class<T> clazz,E e) {
        if (!expression) {
            throwException(clazz,e);
        }
    }

    public static <T extends BaseException,E extends Enum> void isFalse(boolean expression, Class<T> clazz,E e) {
        if (expression) {
            throwException(clazz,e);
        }
    }

    public static <T extends BaseException,E extends Enum> void isNull(Object object, Class<T> clazz,E e) {
        if (object != null) {
            throwException(clazz,e);
        }
    }

    public static <T extends BaseException,E extends Enum> void isNotNull(Object object, Class<T> clazz,E e) {
        if (object == null) {
            throwException(clazz,e);
        }
    }


    public static <T extends BaseException,E extends Enum> void isTrue(boolean expression, String msg) {
        if (!expression) {
            throw new IllegalArgumentException(msg);
        }
    }

    public static <T extends BaseException,E extends Enum> void isFalse(boolean expression, String msg) {
        if (expression) {
            throw new IllegalArgumentException(msg);
        }
    }

    public static <T extends BaseException,E extends Enum> void isNull(Object object, String msg) {
        if (object != null) {
            throw new IllegalArgumentException(msg);
        }
    }

    public static <T extends BaseException,E extends Enum> void isNotNull(Object object, String msg) {
        if (object == null) {
            throw new IllegalArgumentException(msg);
        }
    }

    public static <T extends BaseException, E extends Enum> void isNotBlank(Object object, String msg) {
        if (Func.isEmpty(object)) {
            throw new IllegalArgumentException(msg);
        }
    }
}
